<?php

namespace app\home\controller;

use think\Controller;
use think\Request;

class Login extends Controller
{

    public function login(){
        $this->view->engine->layout(false);
        return view('login');
    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function register(){
        $this->view->engine->layout(false);
        return view('register');

    }
    public function phone(){
        $params = input();
        $validate =    $this->validate($params,[
            'phone'=>'require|regex:/^1[3-9]\d{9}$/|unique:user,phone',
            'code|验证码'=>'require|length:4',
            'password|密码'=>'require|length:6,18|confirm:repassword',

        ]);
      if($validate !== true){
        $this->error($validate);
      }
      $params['password'] = encrypt_password($params['password']);
      $params['username'] = $params['phone'];
      \app\common\model\User::create($params);
      $this->success('注册成功','home/login/login');
    }
    public function sendcode(){
        $params = input();
        $validate = $this->validate($params,[
            'phone'=>'require|regex:/^1[3-9]\d{9}$/',
        ]);
        if($validate!== true){
            $this->error($validate);
            $res = [
                'code'=>400,
                'msg'=>'发送失败',
            ];
            echo json_encode($res);die;
        }
        $code = rand(1000,9999);
        $content = "您的验证码是：{$code}。请不要把验证码泄露给其他人。如非本人操作，可不用理会！";
        $result =sendmsg($params['phone'],$content);
        
        if($result==true){
            $res = [
                'code'=>200,
                'msg'=>'发送成功',
                'data'=>$code
            ];
            echo json_encode($res);die;
        }else{
            $res = [
                'code'=>400,
               'msg'=>'发送失败',
            ];
            echo json_encode($res);die;
        }

    }
}
